﻿#region File and License Information
/*
<File>
	<Copyright>Copyright © 2010, Daniel Vaughan. All rights reserved.</Copyright>
	<License>
		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
		EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="dbvaughan@gmail.com"/>
	<CreationDate>2010-03-21 18:56:56Z</CreationDate>
</File>
*/
#endregion

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace DanielVaughan.Sokoban.UI
{
	public partial class CellControl
	{
		readonly Cell cell;
		readonly ScaleTransform scale;
	
		/// <summary>
		/// Gets the cell that this control displays.
		/// </summary>
		/// <value>The cell.</value>
		public Cell Cell
		{
			get
			{
				return cell;
			}
		}

		public CellControl()
		{
			InitializeComponent();
			Loaded += OnLoaded;
		}

		void OnLoaded(object sender, RoutedEventArgs e)
		{
			UpdateDimensions();
		}

		public void UpdateDimensions()
		{
			wallCell.Visibility = floorCell.Visibility = goalActiveCellContent.Visibility
				= goalCellContent.Visibility = treasureCellContent.Visibility = Visibility.Collapsed;

			/* The dimensions of the root canvas are undefined in the Expression Design export. 
			 * Thus we set them here so that we may scale the control. 800 / .05 = 40 */
			rootCanvas.Width = rootCanvas.Height = 800;
			var scaleAmount = MaxWidth / 800;
			scale.ScaleX = scale.ScaleY = scaleAmount;
			UpdateDisplay();		
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CellControl"/> class.
		/// </summary>
		/// <param name="cell">The associated game cell.</param>
		/// <exception cref="ArgumentNullException">If cell is null.</exception>
		public CellControl(Cell cell) : this()
		{
			if (cell == null)
			{
				throw new ArgumentNullException("cell");
			}

			this.cell = cell;

			scale = new ScaleTransform { ScaleX = 1, ScaleY = 1 };
			rootCanvas.RenderTransform = scale;

			cell.PropertyChanged += cell_PropertyChanged;
			topLayer.MouseLeftButtonUp += topLayer_MouseLeftButtonUp;
			topLayer.MouseEnter += CellControl_MouseEnter;
			topLayer.MouseLeave += CellControl_MouseLeave;
		}

		void topLayer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			OnClick(e);
		}

		void cell_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			switch (e.PropertyName)
			{
				case "CellContents":
					UpdateDisplay();
					break;
			}
		}

		/// <summary>
		/// Shows and hides canvases depending on the cell 
		/// and the cell's contents' state.
		/// </summary>
		void UpdateDisplay()
		{
			wallCell.Visibility = cell is WallCell
				? Visibility.Visible : Visibility.Collapsed;

			floorCellHighlight.Visibility = hasMouse && (cell is FloorCell || cell is GoalCell)
				? Visibility.Visible : Visibility.Collapsed;

			floorCell.Visibility = cell is FloorCell
				? Visibility.Visible : Visibility.Collapsed;

			treasureCellContent.Visibility = cell.CellContents is Treasure
				? Visibility.Visible : Visibility.Collapsed;

			playerCellContent.Visibility = cell.CellContents is Actor
				? Visibility.Visible : Visibility.Collapsed;

			goalCellContent.Visibility = cell is GoalCell && !(cell.CellContents is Treasure)
				? Visibility.Visible : Visibility.Collapsed;

			goalActiveCellContent.Visibility = cell is GoalCell && cell.CellContents is Treasure
				? Visibility.Visible : Visibility.Collapsed;
		}

		bool hasMouse;

		void CellControl_MouseLeave(object sender, EventArgs e)
		{
			hasMouse = false;
			UpdateDisplay();
		}

		void CellControl_MouseEnter(object sender, MouseEventArgs e)
		{
			hasMouse = true;
			UpdateDisplay();
		}

		#region Click event
		event MouseEventHandler click;

		/// <summary>
		/// Occurs when the cell is clicked.
		/// </summary>
		public event MouseEventHandler Click
		{
			add
			{
				click += value;
			}
			remove
			{
				click -= value;
			}
		}

		/// <summary>
		/// Raises the <see cref="E:Click"/> event.
		/// </summary>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void OnClick(MouseEventArgs e)
		{
			if (click != null)
			{
				click(this, e);
			}
		}
		#endregion
	}
}
